package com.yjd.bi.etl.market.service;

import java.util.Calendar;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.yjd.bi.etl.comm.BaseService;
import com.yjd.bi.etl.sql.MarketSqlConstants;
import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.util.BiUtil;
import com.yjd.comm.util.DateUtil;

@Component("marketStatisticService")
public class MarketStatisticService  extends BaseService{
	
	private static final int  DAYOFSECOND = 86400;
	private static final int MARKET_INITIALIZATION_TIME = 1451577600;
	
	public void execute() throws Exception{
		
		BiDatabaseInfoModel model = this.getBiDatabaseInfoModel();
		JdbcTemplate jt = BiUtil.getJdbcTemplate(model);

		// 统计历史数据
		long maxDate = jt.queryForInt(MarketSqlConstants.GET_MAX_STATISTIC_DATE);
		
		if (maxDate == 0) {
			// 没有统计过，取网站上线运营的第一天 平台上线时间（2013年12月21日00：00：00）
			maxDate = MARKET_INITIALIZATION_TIME;
		}else{
			maxDate = DateUtil.dateStringToTimestamp(maxDate+"",
					DateUtil.PATTERN_yyyyMMdd)+DAYOFSECOND;
		}
		
		//获取当天的0点的时间戳
		Calendar todayStart = Calendar.getInstance();  
        todayStart.set(Calendar.HOUR_OF_DAY, 0);  
        todayStart.set(Calendar.MINUTE, 0);  
        todayStart.set(Calendar.SECOND, 0);  
        todayStart.set(Calendar.MILLISECOND, 0);
        long todayTime = todayStart.getTime().getTime()/1000L; 
		
		while (maxDate  < todayTime ) {
			// 统计前一天的用户数据
			statisticLastData(maxDate, jt);
			maxDate += DAYOFSECOND;// 加一天和当前时间比较，如果小于当前时间则继续统计
			
		}
	}

	private void statisticLastData(long maxDate, JdbcTemplate jt) {
		// TODO Auto-generated method stub
		long beginDate = maxDate;
		long endDate = beginDate + DAYOFSECOND - 1;
		
		//遍历渠道
		List<Integer> channel_ids = jt.queryForList(MarketSqlConstants.GET_CHANNEL_ID, Integer.class);
		for (Integer channel_id : channel_ids) {
			
			//注册用户数
			int register_account = jt.queryForObject(MarketSqlConstants.GET_REGISTER_USER_COUNT,new Object[] {channel_id, beginDate, endDate}, Integer.class);
			//充值用户数
			int recharge_account = jt.queryForObject(MarketSqlConstants.GET_RECHARGE_USER_COUNT,new Object[] {channel_id, beginDate, endDate}, Integer.class);
			//投资用户数
			int invest_account = jt.queryForObject(MarketSqlConstants.GET_INVEST_USER_COUNT,new Object[] {beginDate, endDate, beginDate, endDate, beginDate, endDate, channel_id}, Integer.class);
			//充值金额
			double recharge_money =jt.queryForObject(MarketSqlConstants.GET_RECHARGE_MONEY_COUNT,new Object[] {channel_id, beginDate, endDate}, Double.class);
			//投资金额
			double invest_money = jt.queryForObject(MarketSqlConstants.GET_INVEST_MONEY_COUNT,new Object[] {beginDate, endDate, beginDate, endDate, beginDate, endDate, channel_id}, Double.class);
			//统计日期
			Long statics_date = Long.parseLong(DateUtil.timestampToDateString(beginDate, DateUtil.PATTERN_yyyyMMdd));
			
			try{
				jt.update(MarketSqlConstants.MARKET_STATISTIC_INSERTSQL,statics_date,channel_id,register_account,recharge_account,invest_account,recharge_money,invest_money);
			}catch(Exception e){
				logger.info("批量处理插入失败，处理sql:"+MarketSqlConstants.MARKET_STATISTIC_INSERTSQL+"处理数据:"+statics_date+"错误信息:"+e);
			}
		}
	}

}
